package com.studypavilion.mythread;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;



public class MainActivity extends AppCompatActivity {
    private ProgressBar pb1, pb2, pb3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pb1 = findViewById(R.id.progressBar2);
        pb2 = findViewById(R.id.progressBar3);
        pb3 = findViewById(R.id.progressBar4);

        Button btn1 = findViewById(R.id.button);
        Button btn2 = findViewById(R.id.button2);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Android 中开启子线程方式 一
                new Thread(new Runnable() {
//                    重写run方法,也就是重写线程的执行函数
                    @Override
                    public void run() {
                        pgbStart();
                    }
//                    start()开启线程,启动线程,子线程会自动调用run方法运行
                }).start();
            }
        });

    }

    private void pgbStart(){
        int count1 = 0, count2 = 0, count3 = 0;
        while (true){
            count1 += 1;
            count2 += 5;
            count3 += 10;

//            setProgress(); 设置进度条的数值
            pb1.setProgress(count1%100);
            pb2.setProgress(count2%100);
            pb3.setProgress(count3%100);

//            异常：一种错误处理机制，可以保证程序的健壮性和稳定性
//            try {
////                线程中的睡眠, 延迟函数
//                Thread.sleep(500);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }

    }
}